using System;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.CompositeUI;
using Microsoft.Practices.CompositeUI.Commands;
using Jbe.TestSuite.TestDevice.SignalVisualizer.Views;
using Jbe.TestSuite.TestDevice.SignalVisualizer.Constants;
using Jbe.CABExtension.WinForms.Workspaces;
using System.Windows;
using Jbe.TestSuite.TestDevice.SignalVisualizer.Properties;
using Jbe.CABExtension.SmartPartInfos;
using Microsoft.Practices.CompositeUI.EventBroker;
using Jbe.TestSuite.TestDevice.Manager.Interface;
using Jbe.TestSuite.Infrastructure.Interface.Services;

namespace Jbe.TestSuite.TestDevice.SignalVisualizer
{
    public class ModuleController : WorkItemController
    {
        private IUIElementCreationService uiElementCreationService;

        [ServiceDependency]
        public IUIElementCreationService UIElementCreationService
        {
            set { uiElementCreationService = value; }
        }

        public override void Run()
        {
            IMenuItem menuItem = uiElementCreationService.CreateMenuItem(
                "Visualizer", Resources.SignalVisualizer.ToBitmap());
            menuItem.Click += new EventHandler(ShowVisualizerClick);
            WorkItem.UIExtensionSites[UIExtensionSiteNames.ViewMenuItem].Add(menuItem);

            IToolBarItem toolBarItem = uiElementCreationService.CreateToolBarItem(
                "Visualizer", Resources.SignalVisualizer.ToBitmap());
            toolBarItem.Click += new EventHandler(ShowVisualizerClick);

            IToolBar toolBar = uiElementCreationService.CreateToolBar(toolBarItem);
            WorkItem.UIExtensionSites[UIExtensionSiteNames.ToolBarPanel].Add(toolBar);
        }

        #region Command and Event handler

        private void ShowVisualizerClick(object sender, EventArgs e)
        {
            VisualizerView visualizerView = WorkItem.SmartParts.AddNew<VisualizerView>();
            visualizerView.Unloaded += new System.Windows.RoutedEventHandler(VisualizerViewUnloaded);
            IconSmartPartInfo spi = new IconSmartPartInfo("Visualizer", "Visualizer");
            spi.Icon = Resources.SignalVisualizer;
            WorkItem.Workspaces[WorkspaceNames.DockPanelWorkspace].Show(visualizerView, spi);
        }

        private void VisualizerViewUnloaded(object sender, RoutedEventArgs e)
        {
            WorkItem.SmartParts.Remove(sender);
        }

        #endregion
    }
}
